package io.renren.common.dynamicdatasource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author yoqu
 * @date 2017年07月25日
 * @time 下午2:58
 * @email wcjiang2@iflytek.com
 */
@Component
@Aspect
public class DynamicDataSourceAspect {

    @Pointcut("execution(public * io.renren.modules..*Impl.*(..))")
    public void switchDB(){}

    //动态选择切换数据源
    @Before("switchDB()")
    public void beforeSwitchDB(JoinPoint joinPoint){
        Class<?> clazz = joinPoint.getTarget().getClass();
        DynamicSource dynamicSource = clazz.getAnnotation(DynamicSource.class);
        if(dynamicSource==null){
            DataSourceHolder.setDataSource(DataSourceHolder.DATABASE_BASE);
        }else{
            DataSourceHolder.setDataSource(dynamicSource.name());
        }
    }
}
